<?php
require 'vendor/autoload.php';
require 'Service/ApiService.php';
require 'Service/RSAService.php';

//获取余额
function getBalance()
{
    try {
        $api = new ApiService();

        $config = $api->config;

        $sign_type = 'RSA2';

        $data = [
            'api_key' => $config['api_key'],
            'sign_type' => $sign_type,
            'req_str' => uniqid(),
            'timestamp' => time()
        ];

        $data['sign'] = $api->sign($data, $config['private_key'], $sign_type);
        $result = $api->CurlUrl('openapi/v1/balance', $data, 'GET');
        if ($result['code'] == 0) {
            $verfiyStatus = $api->verfiySign($result['data'], $config['platform_public_key'], $result['sign'], $result['sign_type']);
            if ($verfiyStatus) {
                echo '验签成功</br>';
                echo json_encode($result, JSON_UNESCAPED_UNICODE);
            }
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

//获取余额，MD5版本
function getBalanceMd5()
{
    try {
        $api = new ApiService();

        $config = $api->config;

        $sign_type = 'MD5';

        $data = [
            'api_key' => $config['api_key'],
            'sign_type' => $sign_type,
            'req_str' => uniqid(),
            'timestamp' => time()
        ];

        $data['sign'] = $api->sign($data, $config['api_secret'], $sign_type);
        $result = $api->CurlUrl('openapi/v1/balance', $data, 'GET');
        if ($result['code'] == 0) {
            $verfiyStatus = $api->verfiySign($result['data'], $config['platform_public_key'], $result['sign'], $result['sign_type']);
            if ($verfiyStatus) {
                echo '验签成功</br>';
                echo json_encode($result, JSON_UNESCAPED_UNICODE);
            }
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

//创建支付订单
function postOrder()
{
    $api = new ApiService();

    $config = $api->config;

    $sign_type = 'RSA2';

    $data = [
        'api_key' => $config['api_key'],
        'sign_type' => $sign_type,
        'req_str' => uniqid(),
        'timestamp' => time(),
        'pay_type' => 2,
        'out_trade_no' => time(),
        'notify_url' => $config['notify_url'],
        'return_url' => $config['return_url'],
        'money' => 10
    ];

    $data['sign'] = $api->sign($data, $config['private_key'], $sign_type);
    $result = $api->CurlUrl('openapi/v1/order', $data, 'POST');
    if ($result['code'] == 0) {
        $verfiyStatus = $api->verfiySign($result['data'], $config['platform_public_key'], $result['sign'], $sign_type);
        if ($verfiyStatus) {
            echo '验签成功</br>';
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
        }
    }
}


//查询支付订单
function queryOrder()
{
    $api = new ApiService();

    $config = $api->config;

    $sign_type = 'RSA2';

    $data = [
        'api_key' => $config['api_key'],
        'sign_type' => $sign_type,
        'req_str' => uniqid(),
        'timestamp' => time(),
        'out_trade_no' => '1638901208'
    ];

    $data['sign'] = $api->sign($data, $config['private_key'], $sign_type);
    $result = $api->CurlUrl('openapi/v1/order', $data, 'GET');
    if ($result['code'] == 0) {
        $verfiyStatus = $api->verfiySign($result['data'], $config['platform_public_key'], $result['sign'], $sign_type);
        if ($verfiyStatus) {
            echo '验签成功</br>';
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
        }
    }
}

//取消订单
function cacheOrder()
{
    $api = new ApiService();

    $config = $api->config;

    $sign_type = 'RSA2';

    $data = [
        'api_key' => $config['api_key'],
        'sign_type' => $sign_type,
        'req_str' => uniqid(),
        'timestamp' => time(),
        'out_trade_no' => '1638901208'
    ];

    $data['sign'] = $api->sign($data, $config['private_key'], $sign_type);
    $result = $api->CurlUrl('openapi/v1/cacheorder', $data, 'POST');
    if ($result['code'] == 0) {
        $verfiyStatus = $api->verfiySign($result['data'], $config['platform_public_key'], $result['sign'], $sign_type);
        if ($verfiyStatus) {
            echo '验签成功</br>';
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
        }
    }
}

//创建提现订单
function postDrawOrder()
{
    $api = new ApiService();

    $config = $api->config;

    $sign_type = 'RSA2';

    $data = [
        'api_key' => $config['api_key'],
        'sign_type' => $sign_type,
        'req_str' => uniqid(),
        'timestamp' => time(),
        'out_trade_no' => time(),
        'notify_url' => $config['notify_draw_url'],
        'money' => 10,
        'bankcard_name' => '张三',
        'bankcard_account' => '3423423423432423423',
        'bank_name' => '中国银行',
        'bank_province' => '河北省',
        'bank_city' => '石家庄市',
        'bank_address' => '长安区某某路支行',
        'bankcard_type' => 1
    ];

    $data['sign'] = $api->sign($data, $config['private_key'], $sign_type);
    $result = $api->CurlUrl('openapi/v1/orderdraw', $data, 'POST');
    var_dump($result);
    if ($result['code'] == 0) {
        $verfiyStatus = $api->verfiySign($result['data'], $config['platform_public_key'], $result['sign'], $sign_type);
        if ($verfiyStatus) {
            echo '验签成功</br>';
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
        }
    }
}

//查询提现订单
function queryDrawOrder()
{
    $api = new ApiService();

    $config = $api->config;

    $sign_type = 'RSA2';

    $data = [
        'api_key' => $config['api_key'],
        'sign_type' => $sign_type,
        'req_str' => uniqid(),
        'timestamp' => time(),
        'out_trade_no' => '1638953247'
    ];

    $data['sign'] = $api->sign($data, $config['private_key'], $sign_type);
    $result = $api->CurlUrl('openapi/v1/orderdraw', $data, 'GET');
    if ($result['code'] == 0) {
        $verfiyStatus = $api->verfiySign($result['data'], $config['platform_public_key'], $result['sign'], $sign_type);
        if ($verfiyStatus) {
            echo '验签成功</br>';
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
        }
    }
}


postDrawOrder();